﻿using EasyShop.GoodsService.Domain;
using Grpc.Core;
using Infrastructure;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EasyShop.GoodsService.Services
{
    public class GGoodsService:gGoods.gGoodsBase
    {
        // 和ASP.NETCore一样，可以使用依赖注入和服务
        private readonly ILogger<GGoodsService> _logger;
        private readonly MySqlDBContext _mySqlDBContext;
        public GGoodsService(ILogger<GGoodsService> logger,MySqlDBContext mySqlDBContext)
        {
            _logger = logger;
            _mySqlDBContext = mySqlDBContext;
        }

        /// <summary>
        /// 重写 设计对应的多个接口
        /// 一般都是异步处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<GoodsReply> Get(GoodsRequest request, ServerCallContext context)
        {
            var goodsIds = request.Id.Split(",");
            var goods = _mySqlDBContext.Goods.Where(c => goodsIds.Contains(c.Id)).ToList();
            var goodsReplyList =new GoodsReply();
            foreach (var item in goods)
            {
                var goodsReply = item.MapTo<Goods>();
                goodsReplyList.Goods.Add(goodsReply);
            }
            return Task.FromResult(goodsReplyList);
        }
    }
}
